<?php

/**
 * 二维码管理
 * @author Administrator
 */

namespace Partner\Controller;

use Partner\Model\Goods;
use Partner\Model\Export;
use Operation\Model\Code;
use Think\Controller;
use Think\IReq;
use Think\IFilter;
use Think\Page;
use Think\JSON;
use Think\ITime;
use Org\Net\myCode;
use Think\IFile;
use Org\Net\IpLocation;

class CodeController extends ComController {

    public function code_list() {
        
        //搜索条件
        $search = IReq::get('search');
        $p = IReq::get('p') ? IFilter::act(IReq::get('p'), 'int') : 1;
        $seller_id = $this->Seller['seller_id'];
        
        //条件筛选处理
        list($join, $where) = Code::getSearchCondition($search, $seller_id);

        $order = 'c.time DESC, c.addtime DESC, c.id DESC';
        $page = '18';
        $field = 'c.*, go.name as name';
        $codeDB = M('code as c');

        //dump($where);die;
        $codes = $codeDB->join($join)->where($where)->field($field)->order($order)->page($p . ",$page")->select();
        
        $IP = new IpLocation();
        foreach ($codes as $key => $value) {
            $codes[$key]['category'] = Goods::getGoodsCategory($value['good_id']);
            $res = null;
            //首次扫码未获取到地址
            if (!$value['add'] && $value['ip']) {
                $add = $IP->getlocation($value['ip']);
                $codes[$key]['country'] = $add['country'] . $add['area'];
                //$res = strstr($codes[$key]['country'], $value['area']);
                //$codes[$key]['peg'] = $res ? '1' : 0;
            }
            if ($value['add']) {
                $res = strstr($value['add'], $value['area']);
                $codes[$key]['peg'] = $res ? '1' : 0;
            }
        }
        //dump($codeDB->getLastSql());
        //dump($codes);die;
        $count = $codeDB->join($join)->where($where)->count();
        
        //分页跳转的时候保证查询条件
        $param = '';
        foreach ($search as $key => $val) {
            $k = 'search[' . $key . ']';
            if($val){
                $param .= $k . '=' . $val . '&';
            }
        }
        unset($_GET['search']);
        $Page = new Page($count, $page);
        //dump($Page->parameter);die;
        $show = $Page->show($param);
        //print_r($show);die;

        $this->assign('page', $show);
        $this->assign('codes', $codes);
        $this->assign('search', JSON::encode($search));
        $this->display();
    }
    
    public function code_export() {
        $search = IReq::get('search');

        //条件筛选处理
        list($join, $where) = Code::getSearchCondition($search,$this->Seller['seller_id']);

        $order = 'c.addtime DESC, c.id DESC';
        $codeDB = M('code as c');

        $export = new Export();
        if ($search['status'] == '1') {
            $header = array('ID', 'code', '域名+code');
            $field = 'c.id, c.code';
            $url = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . "/index.php/Home/Index/index/code/";
        } else {
            $header = array('ID', '商品名称', '分类', '序列号', '发货地', '添加时间', '扫码次数', '扫码时间', '访问IP', '访问地址');
            $field = 'c.id, go.name, c.good_id, c.code, c.area, c.addtime, c.num, c.time, c.ip, c.add';
        }
        $codes = $codeDB->join($join)->where($where)->field($field)->order($order)->select();
        
        //dump($codes);die;
        $data = array();
        $IP = new IpLocation();
        foreach ($codes as $key => $value) {
            $data[$key] = $value;
            if ($search['status'] == '1') {
                $data[$key]['url'] = $url . $value['code'];
            } else {
                $data[$key]['code'] = $value['code'];
                $data[$key]['addtime'] = ITime::getDateTime('Y-m-d', $value['addtime']);
                $data[$key]['time'] = $value['time'];
                if ($value['good_id']) {
                    $data[$key]['good_id'] = Goods::getGoodsCategory($value['good_id']);
                }
                if (!$value['add'] && $value['ip']) {
                    $add = $IP->getlocation($value['ip']);
                    $data[$key]['ip'] = $value['ip'] . '(' . $add['country'] . $add['area'] . ')';
                }
            }
        }
        //dump($data);die;
        $export->export($data, $header);
    }
    
    //生成二维码
    public function code_edit(){
        $seller_id = $this->Seller['seller_id'];
        $where = array(
            'is_del' => '0',
            'seller_id' => $seller_id
        );
        $goods = M('goods')->field('id,name,spec_array')->where($where)->select();
        foreach ($goods as $key => $value) {
            if($value['spec_array']){
                $arr = JSON::decode($value['spec_array']);
                $i = 0;
                foreach ($arr as $k => $v) {
                    $a[$i] = $v;
                    $a[$i]['value'] = JSON::encode(explode(',', $v['value']));
                    $i++;
                }
                $goods[$key]['spec_array'] = JSON::encode($a);
            } 
        }
        //dump($goods);die;
        $this->assign('json_data', $this->data ? JSON::encode($this->data) : '');
        $this->assign('goods', $goods);
        $this->display();
    }
    
    public function code_save(){
        //dump($_POST);die;
        if($_POST['num']>100){
            $this->data = $_POST;
            $this->error('创建数量不能超过100', U('Code/code_edit')); 
        }
        $res = Code::createCode($_POST);
        if ($res) {
            $this->success('成功生成', U('Code/code_list'));
        } else {
            $this->error('生成失败', U('Code/code_list')); 
        }
    }
    
    public function code_del(){
        //post数据
        $id = IFilter::act(IReq::get('id'), 'int');

        if ($id) {
            $id = is_array($id) ? array('IN', join(',', $id)) : $id;
            $data = array(
                'is_del' => 1,
                'id' => $id
            );
            $codeObj = M('code');
            $codeObj->save($data);
            //dump($goodsObj->getLastSql());die;
            $this->success('删除成功', U('Code/code_list'));
        } else {
            $this->error('请选择要删除的数据', U('Code/code_list'));
        }
    }
    
    /*
     * 二维码扫描次数
     */
    public function code_num(){
        $id = IFilter::act(IReq::get('id'));
        if($id){
            $order = 'num DESC';
            $where = array(
                'code_id' => $id
            );
            $codeObj = M('code_log');
            $data = $codeObj->where($where)->order($order)->select();
        }
        //dump();die;
        $this->assign('data', $data);
        $this->display();
    }
    
    /*
     *  二维码重置
     */
    public function reset() {
        $id = IFilter::act(IReq::get('id'));
        if ($id) {
            $id = is_array($id) ? array('IN', join(',', $id)) : $id;
            $data = array(
                'id' => $id,
                'status' => 1,
                'num' => 0,
                'add' => '',
                'ip' => '',
                'time' => '0000-00-00 00:00:00'
            );
            $res = M('code')->save($data);
            M('code_log')->where(array('code_id' => $id))->delete();
            if ($res) {
                $this->success('重置成功', U('Code/code_list'));
            } else {
                $this->error('重置失败', U('Code/code_list'));
            }
        } else {
            $this->error('请选择要重置的数据', U('Code/code_list'));
        }
    }
    // 获取二维码
    public function get_code() {
        $code = IFilter::act(IReq::get('code'));
        if ($code) {
            $where = array(
                'code' => $code
            );
            $codeObj = M('code');
            $data = $codeObj->where($where)->find();
            $time = $date3 = date('Y/m/d', $data['addtime']);
            $seller_id = M('goods')->where(array('id' => $data['good_id']))->getField("seller_id");
            $path = "./Public/code/" . $seller_id . "/" . $time;
            $img = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . "/Public/code/" . $seller_id . "/" . $time . '/' . md5($code) . ".png";

            if (file_exists($img) === false) {
                if (!is_dir($path)) {
                IFile::mkdir($path);
                }
                $url = "http://" . $_SERVER['SERVER_NAME'] . __ROOT__ . "/index.php/Home/Index/index/code/";
                $dataCode = $url . $code;
                myCode::getInstance()->getCode()->png($dataCode, $path . '/' . md5($code) . ".png");
            }
        }
        $this->assign('img', $img);
        $this->assign('data', $data);
        $this->display();
    }
}